package com.tiger.cloud.controller.visitor;

import javax.servlet.http.HttpServletRequest;

import com.tiger.cloud.model.ext.VisitorPojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.model.visitor.Visitor;
import com.tiger.cloud.service.visitor.IVisitorService;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

/**
 * visit·Ã¿ÍÍ³¼Æ
 */
@Controller
@RequestMapping(value = "visitor")
@Api(value = "访客接口", tags = "访客")
public class VisitorController extends BaseController {

    private static final Logger logger = Logger.getLogger(VisitorController.class);

    @Autowired
    private IVisitorService visitorService;

//    @RequestMapping(value = "find", method = RequestMethod.POST)
//    @ResponseBody
//    public Response find(@CurrentUserId Long userId, @RequestBody Visitor visitor) {
//        Page<Visitor> datas = visitorService.page(visitor);
//        return ResponseUtil.ok(datas);
//    }
//
//
//    @RequestMapping(value = "update", method = RequestMethod.POST)
//    @ResponseBody
//    public Response update(@CurrentUserId Long userId, HttpServletRequest request, @RequestBody Visitor visitor) {
//        visitorService.update(visitor);
//        return ResponseUtil.ok();
//    }

    @RequestMapping(value = "insertVisit", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "添加访客")
    public Response addVisit(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId, @RequestBody Visitor visitor) throws Exception {
        if (visitor.getUserId() == null) {
            return ResponseUtil.error("用户ID不能为空");
        }
        if(visitor.getUserId() == userId){
            return ResponseUtil.ok();
        }
        visitor.setUserId(visitor.getUserId());
        visitor.setVisitId(userId);
        boolean flat = visitorService.insertVisitor(visitor);
        if (flat) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.error();
        }

    }
    @ApiOperation(value = "查询我的访客")
    @RequestMapping(value = "findVisitList", method = RequestMethod.POST)
    @CrossOrigin
    @ResponseBody
    public Response findVisitList(@ApiParam(name="id",value="用户id",hidden=true)@CurrentUserId Long userId,@RequestBody VisitorPojo visitorPojo) {
        if (null == userId) {
            return ResponseUtil.error();
        }
        visitorPojo.setUserId(userId);
        Page<VisitorPojo> result = visitorService.findVisitList(visitorPojo);
        return ResponseUtil.ok(result);
    }

}